package cn.dugcrs.basic.controller;

import cn.dugcrs.basic.dto.RegisterDto;
import cn.dugcrs.basic.exception.BusinessException;
import cn.dugcrs.basic.service.IVerifyCodeService;
import cn.dugcrs.basic.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class ImageVerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 图片验证码生成
     * @param key
     * @return
     */
    @GetMapping("/image/{key}")
    public AjaxResult imgCode(@PathVariable("key") String key){
        try {
            String imgCode = verifyCodeService.getImgCode(key);
            return AjaxResult.success().setResultObj(imgCode);
        } catch (BusinessException e) {     // 自定义异常，处理我们业务层抛出的错误信息
            e.printStackTrace();
            return AjaxResult.error().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /**
     * 发送短信验证码
     * @param dto
     * @return
     */
    @PostMapping("/sendSmsCode")
    public AjaxResult smsCode(@RequestBody RegisterDto dto){
        try {
            verifyCodeService.smsCode(dto);
            return AjaxResult.success();
        } catch (BusinessException e) {     // 自定义异常，处理我们业务层抛出的错误信息
            e.printStackTrace();
            return AjaxResult.error().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * 发送邮箱验证码
     * @param dto
     * @return
     */
    @PostMapping("/emailSendSmsCode")
    public AjaxResult emailSendSmsCode(@RequestBody RegisterDto dto){
        try {
            verifyCodeService.emailSmsCode(dto);
            return AjaxResult.success();
        } catch (BusinessException e) {     // 自定义异常，处理我们业务层抛出的错误信息
            e.printStackTrace();
            return AjaxResult.error().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


}
